package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.search.domain.DestinationEs;
import cn.wolfcode.wolf2w.search.domain.StrategyEs;
import cn.wolfcode.wolf2w.search.domain.TravelEs;
import cn.wolfcode.wolf2w.search.domain.UserInfoEs;
import cn.wolfcode.wolf2w.search.service.IDestinationEsService;
import cn.wolfcode.wolf2w.search.service.IStrategyEsService;
import cn.wolfcode.wolf2w.search.service.ITravelEsService;
import cn.wolfcode.wolf2w.search.service.IUserInfoEsService;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DataController {

    //es服务
    @Autowired
    private IUserInfoEsService userInfoEsService;
    @Autowired
    private IStrategyEsService strategyEsService;
    @Autowired
    private ITravelEsService travelEsService;
    @Autowired
    private IDestinationEsService destinationEsService;


    //mongodb服务
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;

    @GetMapping("/dataInit")
    public String dataInit(){
        //从数据库拿数据
        List<UserInfo> users = userInfoService.list();
        //遍历并拷贝属性到对应的es对象中
        UserInfoEs uses = new UserInfoEs();
        for (UserInfo userInfo : users) {
            BeanUtils.copyProperties(userInfo, uses);
            //将拷贝好的es对象初始化到es中
            userInfoEsService.save(uses);
        }

        //从数据库拿数据
        List<Travel> ts = travelService.list();
        //遍历并拷贝属性到对应的es对象中
        TravelEs tses = new TravelEs();
        for (Travel travel : ts) {
            BeanUtils.copyProperties(travel, tses);
            //将拷贝好的es对象初始化到es中
            travelEsService.save(tses);
        }

        //从数据库拿数据
        List<Strategy> sts = strategyService.list();
        //遍历并拷贝属性到对应的es对象中
        StrategyEs stses = new StrategyEs();
        for (Strategy strategy : sts) {
            BeanUtils.copyProperties(strategy, stses);
            //将拷贝好的es对象初始化到es中
            strategyEsService.save(stses);
        }

        //从数据库拿数据
        List<Destination> dests = destinationService.list();
        //遍历并拷贝属性到对应的es对象中
        DestinationEs destes = new DestinationEs();
        for (Destination dest : dests) {
            BeanUtils.copyProperties(dest, destes);
            //将拷贝好的es对象初始化到es中
            destinationEsService.save(destes);
        }
        return "ok";
    }

}
